home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / tgrind.zip / TFONTEDP.C < prev    next >
C/C++ Source or Header  |  1990-06-29  |  17KB  |  639 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#)@(#)tfontedpr.c    1.3 (LBL) 4/12/85";
  3. #endif
  4.  
  5. /* tfontedpr - general purpose "pretty printer" for use with TeX.
  6.  *
  7.  * Copyright (C) 1985 by Van Jacobson, Lawrence Berkeley Laboratory.
  8.  * This program may be freely used and copied but may not be sold
  9.  * without the author's written permission.  This notice must remain
  10.  * in any copy or derivative.
  11.  *
  12.  * This program is used as part of the "tgrind" shell script.  It
  13.  * converts program source file(s) to TeX input files.
  14.  *
  15.  * This program is an adaptation of "vfontedpr" v4.2 (12/11/84) from
  16.  * the 4.2bsd Unix distribution.  Vfontedpr was written by Dave
  17.  * Presotto (based on an earlier program of the same name written by
  18.  * Bill Joy).
  19.  *
  20.  * I would welcome comments, enhancements, bug fixes, etc.  Please 
  21.  * mail them to:
  22.  *    van@lbl-rtsg.arpa    (from arpanet, milnet, csnet, etc.)
  23.  *    ..!ucbvax!lbl-csam!van    (from Usenet/UUCP)
  24.  *
  25.  * Modifications.
  26.  * --------------
  27.  * 29Jun90  Jim Van Zandt: Appending "\headline={\hfil}" rather than
  28.  *            "\end" to output file, so it can be included in
  29.  *            another TeX file.  Enclosing the output in an
  30.  *            extra set of of braces for the same reason.
  31.  * 25May90  Jim Van Zandt: Allowing for tabwidth other than 8 (e.g., -t4 
  32.  *             switch would set tabwidth to four columns).  Added some
  33.  *            commented braces so the editor's brace matching works.
  34.  * 30Mar85  Chris & Van: Fixed "\C" & "\S" (comment & string start indicators)
  35.  *            to really appear at the start of comments & strings.
  36.  *            Changes for speeded-up expmatch.
  37.  * 29Mar85  Chris Torek (chris@maryland):  Bug fixes for '~' and '^L'
  38.  *            output.  Most cpu-time eaters recoded to improve
  39.  *            efficiency.
  40.  * 10Feb85  Van        Written.
  41.  */
  42.  
  43. #include <ctype.h>
  44. #include <stdio.h>
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47.  
  48. #define boolean int
  49. #define TRUE 1
  50. #define FALSE 0
  51. #define NIL 0
  52. #define STANDARD 0
  53. #define ALTERNATE 1
  54.  
  55. #define STRLEN 10        /* length of strings introducing things */
  56. #define PNAMELEN 80        /* length of a function/procedure name */
  57. #define PSMAX 20        /* size of procedure name stacking */
  58.  
  59. /* regular expression routines */
  60.  
  61. char    *expmatch();        /* match a string to an expression */
  62. char    *convexp();        /* convert expression to internal form */
  63. char    *tgetstr();
  64.  
  65. boolean    isproc();
  66.  
  67.  
  68. char    *ctime();
  69.  
  70. /*
  71.  *    The state variables
  72.  */
  73.  
  74. boolean    incomm;            /* in a comment of the primary type */
  75. boolean    instr;            /* in a string constant */
  76. boolean    inchr;            /* in a string constant */
  77. boolean    nokeyw = FALSE;        /* no keywords being flagged */
  78. boolean prccont;        /* continue last procedure */
  79. int    comtype;        /* type of comment */
  80. int    psptr;            /* the stack index of the current procedure */
  81. char    pstack[PSMAX][PNAMELEN+1];    /* the procedure name stack */
  82. int    plstack[PSMAX];        /* the procedure nesting level stack */
  83. int    blklevel;        /* current nesting level */
  84. char    *defsfile = DEFSFILE;    /* name of language definitions file */
  85. char    pname[BUFSIZ+1];
  86. int    tabwidth = 8;        /* assumed width of a tab character */
  87.  
  88. /*
  89.  *    The language specific globals
  90.  */
  91.  
  92. char    *language = "c";    /* the language indicator */
  93. char    *l_keywds[BUFSIZ/2];    /* keyword table address */
  94. char    *l_prcbeg;        /* regular expr for procedure begin */
  95. char    *l_combeg;        /* string introducing a comment */
  96. char    *l_comend;        /* string ending a comment */
  97. char    *l_acmbeg;        /* string introducing a comment */
  98. char    *l_acmend;        /* string ending a comment */
  99. char    *l_blkbeg;        /* string begining of a block */
  100. char    *l_blkend;        /* string ending a block */
  101. char    *l_strbeg;        /* delimiter for string constant */
  102. char    *l_strend;        /* delimiter for string constant */
  103. char    *l_chrbeg;        /* delimiter for character constant */
  104. char    *l_chrend;        /* delimiter for character constant */
  105. char    l_escape;        /* character used to escape characters */
  106. boolean    l_toplex;        /* procedures only defined at top lex level */
  107. boolean    l_onecase;        /* upper & lower case equivalent */
  108.  
  109. /*
  110.  *  global variables also used by expmatch
  111.  */
  112. extern    boolean    _escaped;    /* if last character was an escape */
  113. extern    char *_start;        /* start of the current string */
  114.  
  115. int    (*re_strncmp)();    /* function to do string compares */
  116. extern    int strncmp();
  117. extern    int lc_strncmp();
  118.  
  119. /*
  120.  * The following table converts ASCII characters to a printed
  121.  * representation, taking care of all the TeX quoting.  N.B.: all
  122.  * single-character strings are assumed to be equivalent to the
  123.  * character for that index (i.e., printtab['c'] can't be "f").
  124.  * (This is purely for efficiency hacking.)
  125.  */
  126. char *printtab[128] = {
  127.     "\0x",   "\\^A",  "\\^B",  "\\^C",  "\\^D",  "\\^E",  "\\^F",  "\\^G",
  128.     "\\^H",  "\t",    "}}\n",  "\\^K",  "\0x",   "\\^M",  "\\^N",  "\\^O",
  129.     "\\^P",  "\\^Q",  "\\^R",  "\\^S",  "\\^T",  "\\^U",  "\\^V",  "\\^W",
  130.     "\\^X",  "\\^Y",  "\\^Z",  "\\^[",  "\\^\\!","\\^]",  "\\^\\^","\\^_",
  131.     " ",     "!",     "\\\"",  "\\#",   "\\$",   "\\%",   "\\&",   "\\'",
  132.     "(",     ")",     "*",     "+",     ",",     "\\-",   ".",     "\\/",
  133.     "0",     "1",     "2",     "3",     "4",     "5",     "6",     "7",
  134.     "8",     "9",     ":",     ";",     "\\<",   "=",     "\\>",   "?",
  135.     "@",     "A",     "B",     "C",     "D",     "E",     "F",     "G",
  136.     "H",     "I",     "J",     "K",     "L",     "M",     "N",     "O",
  137.     "P",     "Q",     "R",     "S",     "T",     "U",     "V",     "W",
  138.     "X",     "Y",     "Z",     "[",     "\\!",   "]",     "\\^",   "\\_",
  139.     "`",     "a",     "b",     "c",     "d",     "e",     "f",     "g",
  140.     "h",     "i",     "j",     "k",     "l",     "m",     "n",     "o",
  141.     "p",     "q",     "r",     "s",     "t",     "u",     "v",     "w",
  142.     "x",     "y",     "z",     "\\{",   "\\|",   "\\}",   "\\~",   "\\^?",
  143. };
  144.  
  145. /* Output a character, with translation.  Avoid side effects with this
  146.    macro! */
  147. #define outchar(c) (printtab[c][1] ? printf("%s", printtab[c]) : putchar(c))
  148.  
  149. /*
  150.  * Output a TeX command to tab to column "col" (see tgrindmac.tex for a 
  151.  * partial explanation of the bizarre brace arrangement).
  152.  */
  153. #define tabto(col) printf("}\\Tab{%d}{", col);
  154.  
  155. main(argc, argv)
  156.     int argc;
  157.     register char *argv[];
  158. {
  159.     char *fname = "", *p;
  160.     struct stat stbuf;
  161.     char buf[BUFSIZ];
  162.     char strings[2 * BUFSIZ];
  163.     char defs[2 * BUFSIZ];
  164.  
  165. #ifdef __TURBOC__        /* "tgrindmac" -> "tgrind" for PC's short filenames */
  166.     printf("\\input tgrind\n{\n"); 
  167. #else
  168.     printf("\\input tgrindmac\n{\n");
  169. #endif
  170.     argc--, argv++;
  171.     do {
  172.     register char *cp;
  173.     register int i;
  174.  
  175.     if (argc > 0) {
  176.         if (!strcmp(argv[0], "-h")) {
  177.         if (argc == 1) {
  178.             printf("\\Head{}\n");
  179.             argc = 0;
  180.             goto rest;
  181.         }
  182.         printf("\\Head{");
  183.         putstr( argv[1] );
  184.         printf( "}\n" );
  185.         argc--, argv++;
  186.         argc--, argv++;
  187.         if (argc > 0)
  188.             continue;
  189.         goto rest;
  190.         }
  191.  
  192.         /* take input from the standard place */
  193.         if (!strcmp(argv[0], "-")) {
  194.         argc = 0;
  195.         goto rest;
  196.         }
  197.  
  198.         /* indicate no keywords */
  199.         if (!strcmp(argv[0], "-n")) {
  200.         nokeyw++;
  201.         argc--, argv++;
  202.         continue;
  203.         }
  204.  
  205.         /* specify the language */
  206.         if (!strncmp(argv[0], "-l", 2)) {
  207.         language = argv[0]+2;
  208.         argc--, argv++;
  209.         continue;
  210.         }
  211.  
  212.         /* specify the language description file */
  213.         if (!strncmp(argv[0], "-d", 2)) {
  214.         defsfile = argv[1];
  215.         argc--, argv++;
  216.         argc--, argv++;
  217.         continue;
  218.         }
  219.  
  220.         /* set the tabwidth */
  221.         if (!strncmp(argv[0], "-t", 2)) {
  222.         tabwidth = atoi(argv[0]+2);
  223.         if(tabwidth < 1 || tabwidth > 64) tabwidth = 8;
  224.         argc--, argv++;
  225.         continue;
  226.         }
  227.  
  228.         /* open the file for input */
  229.         if (freopen(argv[0], "r", stdin) == NULL) {
  230.         perror(argv[0]);
  231.         exit(1);
  232.         }
  233.  
  234.         fname = argv[0];
  235.         argc--, argv++;
  236.     }
  237.     rest:
  238.  
  239.     /*
  240.      *  get the  language definition from the defs file
  241.      */
  242.     i = tgetent (defs, language, defsfile);
  243.     if (i == 0) {
  244.         fprintf (stderr, "no entry for language %s\n", language);
  245.         exit (0);
  246.     } else  if (i < 0) {
  247.         fprintf (stderr,  "cannot find vgrindefs file %s\n", defsfile);
  248.         exit (0);
  249.     }
  250.     p = strin